package com.athuawei.redis_springboot.controller;

import com.athuawei.redis_springboot.service.SecKillService;
import com.athuawei.redis_springboot.service.SecKillService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

@Controller
public class RedisController {

    @Autowired
    private RedisTemplate redisTemplate;

//    @Autowired
//    SecKillService secKillService;

    @Autowired
    SecKillService2 secKillService2;

    @GetMapping("/redis")
    @ResponseBody
    public String testRedis() {

        // 设置值到redis
        redisTemplate.opsForValue().set("svn", "git");

        // 从redis获取值
        String svn = (String) redisTemplate.opsForValue().get("svn");

        return svn;
    }

//    @GetMapping("/seckill")
//    public String seckill() {
//
//        return "seckill";
//    }

//    @PostMapping("/doseckill")
//    public void seckill(HttpServletRequest request, HttpServletResponse response) throws IOException {
//
//        String userid = new Random().nextInt(50000) + "";
//        String prodid = request.getParameter("prodid");
//
//
//        boolean isSuccess = secKillService.doSecKill(userid, prodid);
//        response.getWriter().print(isSuccess);
//    }

    @GetMapping("/seckill2")
    public String seckill2() {

        return "seckill2";
    }


    @PostMapping("/doseckill2")
    public void seckill2(HttpServletRequest request, HttpServletResponse response) throws IOException {

        String userid = new Random().nextInt(50000) + "";
        String prodid = request.getParameter("prodid");


        boolean isSuccess = secKillService2.doSecKill(userid, prodid);
        response.getWriter().print(isSuccess);
    }
}
